package cn.com.hhrcw.web;

import cn.com.hhrcw.entity.CvResumeStatistics;
import cn.com.hhrcw.mapstruct.CvResumeStatisticsConvert;
import cn.com.hhrcw.params.CvResumeStatisticsParams;
import cn.com.hhrcw.service.ICvResumeStatisticsService;
import cn.com.hhrcw.utils.ExcelUtil;
import cn.com.hhrcw.vo.CvResumeStatisticsVO;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.AttributesValueParams;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.function.Function;

/**
 * @author jiangdong
 * @since 2020-11-18
 */
@RestController
@Slf4j
@RequestMapping("/cvResumeStatistics")
@Api(tags = "")
@Validated
public class CvResumeStatisticsController extends JeecgController<CvResumeStatistics, ICvResumeStatisticsService> {

    @Autowired
    private ICvResumeStatisticsService cvResumeStatisticsService;

    @Autowired
    private CvResumeStatisticsConvert cvResumeStatisticsConvert;


    /**
     * 数据报表-简历量统计-下面的列表
     *
     * @param params 请求参数
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PermissionData
    public Result<Page<CvResumeStatisticsVO>> list(CvResumeStatisticsParams params, HttpServletRequest request) {
        LambdaQueryWrapper<CvResumeStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CvResumeStatistics::getDelFlag, false);
        if (params.getStartTime() != null) {
            queryWrapper.ge(CvResumeStatistics::getCreateTime, params.getStartTime());
        }
        if (params.getEndTime() != null) {
            queryWrapper.le(CvResumeStatistics::getCreateTime, params.getEndTime());
        }
        queryWrapper.orderByDesc(CvResumeStatistics::getCreateTime);
        IPage<CvResumeStatistics> page = cvResumeStatisticsService.page(params.page(), queryWrapper);
        //分页结果转换器
        Function<CvResumeStatistics, CvResumeStatisticsVO> fun = cvResumeStatistics -> cvResumeStatisticsConvert.toVO(cvResumeStatistics);
        return Result.ok((Page) page.convert(fun));
    }

    /**
     * 根据id查询
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/queryById")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @PermissionData
    public Result<CvResumeStatisticsVO> queryById(String id) {
        CvResumeStatistics cvResumeStatistics = cvResumeStatisticsService
                .lambdaQuery()
                .eq(CvResumeStatistics::getId, id)
                .eq(CvResumeStatistics::getDelFlag, false)
                .one();
        if (cvResumeStatistics == null) {
            throw new JeecgBootException("数据不存在");
        }
        return Result.ok(cvResumeStatisticsConvert.toVO(cvResumeStatistics));
    }

    /**
     * 添加
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加", notes = "添加")
    @RequiresPermissions("cvResumeStatistics:add")
    public Result<?> add(@RequestBody @Valid CvResumeStatisticsParams params) {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CvResumeStatistics cvResumeStatistics = cvResumeStatisticsConvert.toEntity(params);
        cvResumeStatistics.setCreateBy(sysUser.getId());
        cvResumeStatistics.setCreateTime(DateUtil.date());
        cvResumeStatistics.setDelFlag(false);

        //cvResumeStatistics.setNo(super.sort("cv_resume_statistics", Constant.NO_SEED));//FIXME: 编号,可根据情况隐藏
        boolean saved = cvResumeStatisticsService.save(cvResumeStatistics);
        return saved ? Result.ok() : Result.error("添加失败");
    }

    /**
     * 编辑
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "编辑", notes = "编辑")
    @RequiresPermissions("cvResumeStatistics:edit")
    public Result<?> edit(@RequestBody @Valid CvResumeStatisticsParams params) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        CvResumeStatistics cvResumeStatistics = cvResumeStatisticsConvert.toEntity(params);
        cvResumeStatistics.setUpdateBy(sysUser.getId());
        cvResumeStatistics.setUpdateTime(DateUtil.date());

        boolean updated = cvResumeStatisticsService.updateById(cvResumeStatistics);
        return updated ? Result.ok() : Result.error("编辑失败");
    }


    /**
     * 批量删除
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/delete")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @RequiresPermissions("cvResumeStatistics:delete")
    public Result delete(String[] id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaUpdateWrapper<CvResumeStatistics> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(CvResumeStatistics::getId, id)
                .set(CvResumeStatistics::getDelFlag, true)
                .set(CvResumeStatistics::getUpdateBy, sysUser.getUsername())
                .set(CvResumeStatistics::getUpdateTime, DateUtil.date());

        boolean deleted = cvResumeStatisticsService.update(updateWrapper);
        return deleted ? Result.ok() : Result.error("删除失败");
    }

    /**
     * 属性值是否重复
     *
     * @return
     */
    @Override
    @GetMapping("/propertyRepeat")
    @ApiOperation(value = "属性值是否重复", notes = "属性值是否重复")
    public Result propertyIsRepeat(@Valid AttributesValueParams params) {
        return super.propertyIsRepeat(params);
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CvResumeStatistics.class);
    }

}
